package com.iqiyi.pps.epg.core.service.statistic;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.statistic.*;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticSty2Data;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.statistic.StatisticSty2DataDao;
import com.iqiyi.pps.epg.core.model.statistic.StatisticSty2Data;
import com.iqiyi.pps.epg.core.utils.MathUtils;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by zhang on 2016/4/21.
 */
@Service
@Transactional
public class StatisticSty2DataServiceImpl implements StatisticSty2DataService {

    @Autowired
    private StatisticSty2DataDao dao;

    @Override
    public TStatisticSty2DataPage getPage(TPage page, TFilter filter) {
        return null;
    }

    @Override
    public void save(StatisticSty2Data entity) {
        if (entity == null)
            return;
        dao.save(entity);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public TStatisticSty2DataPage findTStatisticSty2DataPage(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        String tempPattern = "${PARAMS}";
        String totalPattern = "select COUNT(*)";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        String hql = tempPattern + " from StatisticSty2Data";
        Page<StatisticSty2Data> dbPage = TransUtil.buildPage(page);

        hql = TransUtil.buildQueryStringByPropertyFilter(dbPage, hql, filterList);

        if (dbPage.isAutoCount()) {
            String totalCount = dao.getSession().createQuery(hql.replace(tempPattern, totalPattern)).uniqueResult().toString();
            page.setTotalCount(Long.valueOf(totalCount));
        }

        List result = dao.getSession().createQuery(hql.replace(tempPattern, ""))
                .setMaxResults(dbPage.getPageSize())
                .setFirstResult((dbPage.getPageNo() - 1) * dbPage.getPageSize())
                .list();
        TStatisticSty2DataPage tStatisticSty2DataPage = new TStatisticSty2DataPage();
        tStatisticSty2DataPage.setTPage(page);
        List<TStatisticSty2Data> tStatisticSty2Datas = new ArrayList<TStatisticSty2Data>();
        for (Object obj : result) {
            StatisticSty2Data statisticSty2Data = (StatisticSty2Data) obj;
            tStatisticSty2Datas.add(statisticSty2Data.convertThriftModel());
        }
        tStatisticSty2DataPage.setResult(tStatisticSty2Datas);
        return tStatisticSty2DataPage;
    }

    @Override
    public List<TStatisticSty2PercentData> findTStatisticSty2PercentData(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        StringBuffer _hql = new StringBuffer().append("select sty2.channelId,sty2.channelName,sty2.categoryName,sty2.albumName, ")
                .append("sum(sty2.headerCompleteNumbers) as headerCompleteCount, ")
                .append("sum(sty2.headerIncompleteNumbers) as headerIncompleteCount, ")
                .append("sum(sty2.headerFaultNumbers) as headerFaultCount, ")
                .append("sum(sty2.lastCompleteNumbers) as lastCompleteCount, ")
                .append("sum(sty2.lastIncompleteNumbers) as lastIncompleteCount, ")
                .append("sum(sty2.lastFaultNumbers) as lastFaultCount, ")
                .append("sum(sty2.adCompleteNumbers) as adCompleteCount, ")
                .append("sum(sty2.adIncompleteNumbers) as adIncompleteCount, ")
                .append("sum(sty2.adFaultNumbers) as adFaultCount ")
                .append("from StatisticSty2Data sty2");

        Page<StatisticSty2Data> dbPage = TransUtil.buildPage(page);
        String hql = TransUtil.buildQueryStringByPropertyFilter(null, _hql.toString(), filterList);

        hql = hql + "group by sty2.channelId,sty2.channelName,sty2.albumName,sty2.categoryName order by sty2.channelName asc";
        List<Object[]> result = dao.getSession().createQuery(hql).list();

        List<TStatisticSty2PercentData> list = new ArrayList<TStatisticSty2PercentData>();
        for (Object[] objects : result) {
            TStatisticSty2PercentData data = new TStatisticSty2PercentData();
            data.setChannelId(objects[0].toString());
            data.setChannelName(objects[1].toString());
            data.setCategoryName(objects[2].toString());
            data.setAlbumName(objects[3].toString());
            long hcn = (Long) objects[4];
            long hin = (Long) objects[5];
            long hfn = (Long) objects[6];
            long lcn = (Long) objects[7];
            long lin = (Long) objects[8];
            long lfn = (Long) objects[9];
            long acn = (Long) objects[10];
            long ain = (Long) objects[11];
            long afn = (Long) objects[12];

            long total = hcn + hin + hfn;
            String hcp = MathUtils.percent(hcn, total, 0);
            String hip = MathUtils.percent(hin, total, 0);
            String hfp = MathUtils.percent(hfn, total, 0);

            total = lcn + lin + lfn;
            String lcp = MathUtils.percent(lcn, total, 0);
            String lip = MathUtils.percent(lin, total, 0);
            String lfp = MathUtils.percent(lfn, total, 0);

            total = acn + ain + afn;
            String acp = MathUtils.percent(acn, total, 0);
            String aip = MathUtils.percent(ain, total, 0);
            String afp = MathUtils.percent(afn, total, 0);

            data.setHeaderCompletePercent( hcp + "(" + hcn + ")");
            data.setHeaderIncompletePercent(hip + "(" + hin + ")");
            data.setHeaderFaultPercent(hfp + "(" + hfn + ")");
            data.setLastCompletePercent(lcp + "(" + lcn + ")");
            data.setLastIncompletePercent(lip + "(" + lin + ")");
            data.setLastFaultPercent(lfp + "(" + lfn + ")");
            data.setAdCompletePercent(acp + "(" + acn + ")");
            data.setAdIncompletePercent(aip + "(" + ain + ")");
            data.setAdFaultPercent(afp + "(" + afn + ")");

            list.add(data);
        }
        return list;
    }

    @Override
    public List<String> suggest(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        String field = filterList.get(0).getPropertyName();
        StringBuffer _hql = new StringBuffer().append("select distinct sty2.").append(field)
                .append(" from StatisticSty2Data sty2");

        Page<StatisticSty2Data> dbPage = TransUtil.buildPage(page);
        String hql = TransUtil.buildQueryStringByPropertyFilter(dbPage, _hql.toString(), filterList);

        List<String> result = dao.getSession().createQuery(hql).list();
        return result;
    }
}
